<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-28
 * Time: 11:17
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\GoodsSort;
use app\api\model\Goods as GoodsModel;

class Goods extends BaseController
{
    protected $beforeActionList = [
        'checkPrimaryScope' => ['only' => 'getSortDataAll,getGoodsRecommendAll,getGoodsData,getGoodsInfo']
    ];

    // 获取商品分类
    public function getSortDataAll()
    {
//        return GoodsSort::where('id','lt',9)->with('img')->order('order_sort desc')->select()->toArray();
        return GoodsSort::with('img')->order('order_sort desc')->select()->toArray();
    }

    // 商品推荐列表
    public function getGoodsRecommendAll()
    {
        return GoodsSort::with('recommends.image.img')
//        return GoodsSort::where('id','lt',9)->with('recommends.image.img')
            ->order('order_sort desc')
            ->select()->toArray();
    }

    // 获取商品列表
    public function getGoodsData($sort_id)
    {
        if($sort_id) {
            return GoodsModel::with('image.img')->where('up_down',1)->where('sort_id',$sort_id)->order('order_sort desc')->select()->toArray();
        } else {
            return GoodsModel::with('image.img')->where('up_down',1)->order('order_sort desc')->select()->toArray();
        }
    }

    // 获取商品详情
    public function getGoodsInfo($id)
    {
        return GoodsModel::with('images.img')->where('id',$id)->find();
    }
}